//
//  KeyboardEmoticonPackage.swift
//  customKeyboard
//
//  Created by changhongliang on 2017/8/12.
//  Copyright © 2017年 richinfo. All rights reserved.
//

import UIKit

class KeyboardEmoticonPackage: NSObject {
    ///当前组对应的文件夹名字
    var id: String
    ///当前组的名称
    var group_name_cn: String?
    ///当前组所有的表情模型
    var emotions: [KeyboardEmoticon]?
    init(id:String){
        self.id = id
    }
    
    //加载所有组的表情
    class func loadPackages()->[KeyboardEmoticonPackage]{
    
        //1.获取emoticons.plist的路径
        let path = Bundle.main.path(forResource: "emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")
        //2.根据路径加载plist文件
        let dict = NSDictionary(contentsOfFile: path!)
        //3.取出所有组的字典
        let packages = dict?["packages"] as![[String:AnyObject]]
        //4.遍历数组,取出每一组对应的文件夹名称
        var models = [KeyboardEmoticonPackage]()
        for package in packages {
            let emotionPackage = KeyboardEmoticonPackage(id: package["id"] as! String)
            //加载这一组的表情
            emotionPackage.loadEmotions()
//            models.append(emotionPackage)
            // 4.3追加空白按钮
            emotionPackage.appendEmptyEmoticons()
            // 4.3将当前组添加到数组中
            models.append(emotionPackage)
        }
        //初始化最近这一组
        let package = KeyboardEmoticonPackage(id: "zuijin")
        package.appendEmptyEmoticons()
        package.emotions?.removeLast()
        package.emotions?.append(KeyboardEmoticon(id:"zuijin"))
        models.insert(package, at: 0)
        return models
    }
    
    ///追加空白表情
    private func appendEmptyEmoticons(){
        if self.emotions == nil {
            self.emotions = [KeyboardEmoticon]()
            self.emotions?.append(KeyboardEmoticon(id:""))
        }
        var count = (self.emotions?.count)! % 21
        while count < 20 && count != 0
        {
           emotions?.append(KeyboardEmoticon(id: self.id))
           count += 1
        }
        emotions?.append(KeyboardEmoticon(id: self.id, removeFlag: true))
    
    }
   
    /// 加载当前组的所有表情
   private func loadEmotions(){
        //1.拼接info.plist路径
        //1.1 获取对应组的文件夹路径
    let path = Bundle.main.path(forResource: id, ofType: nil, inDirectory: "Emoticons.bundle")

        //1.2拼接info.plist路径
    let filePath = (path! as NSString).appendingPathComponent("info.plist")
        //2.加载info.plist
    let dict = NSDictionary(contentsOfFile: filePath)!
    //3.取出组名
    group_name_cn = dict[" group_name_cn"] as? String
    //4.取出当前组中所有的表情"字典数组"
    let emotions = dict["emoticons"] as! [[String: AnyObject]]
    var models = [KeyboardEmoticon]()
    //5.遍历取出每一个表情
   
    for  (index, emotionDict) in emotions.enumerated() {
        if index != 0 && index % 20 == 0 {
            models.append(KeyboardEmoticon(id: self.id, removeFlag: true))
        }
        let emoticon = KeyboardEmoticon(id: self.id)
        emoticon.chs = emotionDict["chs"] as? String
        emoticon.png = emotionDict["png"] as? String
        emoticon.code = emotionDict["code"] as? String
        models.append(emoticon)
    }
    //6.将加载的表情赋值给当前的组模型
    self.emotions = models
        
    }
    
    /// 添加表情到最近
    func addFavoriteEmoticon(emoticon: KeyboardEmoticon)
    {
        // 1.判断当前传入的表情是否已经存在
        let contains = emotions!.contains(emoticon)
        
        // 删除删除按钮
        emotions?.removeLast()
        
        // 2.如果没有添加过就添加
        if !contains
        {
            // 删除一个表情
            emotions?.removeLast()
            // 添加一个表情
//            emotions?.append(emoticon)
            emotions?.insert(emoticon, at: 0)
        }
        
        // 3.排序(根据使用次数)
//        let array:[KeyboardEmoticon] = emotions?.sort(by: { (e1, e2) -> Bool in
//            return e1.count > e2.count
//        })
//        emotions = array
        // 4.添加一个删除按钮
        emotions?.append(KeyboardEmoticon(id: "", removeFlag: true))
    }
    

}


//保存一个表情模型
class KeyboardEmoticon: NSObject{
    ///当前表情对应的文件夹名称
    var id: String
    ///当前表情对应的文字
    var chs: String?
    ///当前表情对应的图片名称
    var png:String?{
        didSet{
            guard let temp = png else {
                return
            }
            //1.1获取对应组的文件路径
            let path = Bundle.main.path(forResource: id, ofType: nil, inDirectory: "Emoticons.bundle")!
            //1.2拼接图片全路径
            imagePath = (path as NSString).appendingPathComponent(temp)
        }
    
    }
    ///当前表情对应的图片全路径
    var imagePath: String?
    
    ///Emoji表情对应的十六进制字符串
    var code: String?{
        didSet{
            //1.创建一个扫描器
            let scanner = Scanner(string: code ?? "")
            //2.从字符串中扫描对应的十六机制
            var res: uint = 0
            scanner.scanHexInt32(&res)
            //3.根据十六进制创建一个表情字符串
            codeStr = "\(Character(UnicodeScalar(res)!))"
            
        
        }
    
    }
    ///Emoji表情
    var codeStr:String?
    
    /// 记录当前表情是否是删除按钮
    var isRemoveButton: Bool
    
    /// 使用次数
    var count: Int = 0
    
    init(id: String, removeFlag: Bool = false)
    {
        self.id = id
        self.isRemoveButton = removeFlag
    }


}
